package com.spring.nbrbt.tools.otherutil;

import org.springframework.data.mongodb.core.MongoTemplate;

import com.spring.security.web.model.Log;
import com.spring.security.web.service.MessageProvider;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2014-11-7
 */
public class LogUtils {
	
	public static final String CACHE_MENU_NAME_PATH_MAP = "menuNamePathMap";
	
//	private static LogDao logDao = SpringContextHolder.getBean(LogDao.class);
//	private static MenuDao menuDao = SpringContextHolder.getBean(MenuDao.class);
	
	private static MongoTemplate mongoService = SpringContextHolder.getBean(MongoTemplate.class);
	private static MessageProvider messageProvider = SpringContextHolder.getBean(MessageProvider.class);
	
	
	/**
	 * 保存日志
	 */
	public static void saveLog(Log log, String title){
		saveLog(log, null, null, title);
	}
	
	/**
	 * 保存日志
	 */
	public static void saveLog(Log log, Object handler, Exception ex, String title){
//		User user = UserUtils.getUser();
//		if (user != null && user.getId() != null){
		if(ex!=null){ 
			log.setException(ex.toString());
			//如有异常，则发送邮件
			messageProvider.emailProvider(log);
		}
		mongoService.insert(log);
//			// 异步保存日志
//			new SaveLogThread(log, handler, ex).start();
//		}
	}
	
	public static void sendExceptionMail(Log log){
		SendMailLogThread st = new SendMailLogThread(log);
		st.run();
//		SendMailUtil.sendCommonMail("defsakl123@126.com", "异常通知!", log.toString());
	}
	
	
	/**
	 * 发送异常邮件
	 */
	public static class SendMailLogThread extends Thread{
		
		private Log log;
		
		public SendMailLogThread(Log log){
			super(SendMailLogThread.class.getSimpleName());
			this.log = log;
		}
		
		@Override
		public void run() {
			SendMailUtil.sendCommonMail("499699448@qq.com", "异常通知!", log.toString());
		}
	}

}
